package cn.com.dingtek.genius.manager.server.service;

import cn.com.dingtek.genius.manager.server.repository.db.entity.DeviceBase;
import cn.com.dingtek.genius.manager.server.repository.db.entity.TrashcanLocation;
import cn.com.dingtek.genius.manager.server.repository.db.manager.DeviceBaseManager;
import cn.com.dingtek.genius.manager.server.repository.db.manager.TrashcanLocationManager;
import cn.com.dingtek.genius.manager.server.utils.MybatisPlusUtil;
import cn.com.dingtek.genius.manager.server.utils.Verify;
import cn.com.dingtek.genius.manager.server.utils.VerifyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TrashcanLocationService {
    @Autowired
    private TrashcanLocationManager trashcanLocationManager;
    @Autowired
    private DeviceBaseManager deviceBaseManager;


    public IPage<TrashcanLocation> list(TrashcanLocation param, Integer searchState, int currentPage, int pageSize) {
        QueryWrapper<TrashcanLocation> query = Wrappers.emptyWrapper();
        if (searchState != null) {
            if (searchState == 1) {
                query = Wrappers.<TrashcanLocation>query().isNotNull("imei");
            } else if (searchState == 0) {
                query = Wrappers.<TrashcanLocation>query().isNull("imei");
            }
        }

        if (param != null) {
            query = MybatisPlusUtil.customLikeQuery(query, "location_name", param.getLocationName());
            query = MybatisPlusUtil.customLikeQuery(query, "trashcan_type", param.getTrashcanType());
            query = MybatisPlusUtil.customEqQuery(query, "volume", param.getVolume());
        }

        IPage<TrashcanLocation> staffBasePage = trashcanLocationManager.page(new Page<TrashcanLocation>(currentPage, pageSize).setDesc("id"), query);
        return staffBasePage;
    }


    public void add(TrashcanLocation param, String currentAccount, Date now) {
        try {
            Verify.isTrue(trashcanLocationManager.save(TrashcanLocation.builder()
                    .locationName(param.getLocationName())
                    .trashcanType(param.getTrashcanType())
                    .volume(param.getVolume())
                    .lng(param.getLng())
                    .lat(param.getLat())
                    .remarks(param.getRemarks())
                    .valid(true)
                    .createUser(currentAccount)
                    .updateUser(currentAccount)
                    .ctime(now)
                    .utime(now)
                    .version(0)
                    .build()), "新增垃圾箱位置失败.");
        } catch (DuplicateKeyException e) {
            throw new VerifyException("该垃圾箱位置已存在");
        }
    }

    public TrashcanLocation info(Long id) {
        Verify.isTrue(id != null && id > 0, "垃圾箱位置不存在");
        return trashcanLocationManager.getById(id);
    }

    public void update(TrashcanLocation param, String currentAccount, Date now) {
        Verify.isTrue(param.getId() != null && param.getId() > 0, "垃圾箱位置不存在");

        TrashcanLocation trashcanLocation = trashcanLocationManager.getById(param.getId());
        Verify.notNull(trashcanLocation, "垃圾箱位置不存在");

        TrashcanLocation toUpdate = TrashcanLocation.builder()
                .id(param.getId())
                .locationName(param.getLocationName())
                .trashcanType(param.getTrashcanType())
                .volume(param.getVolume())
                .lng(param.getLng())
                .lat(param.getLat())
                .remarks(param.getRemarks())
                .updateUser(currentAccount)
                .utime(now)
                .version(trashcanLocation.getVersion()).build();

        Verify.isTrue(trashcanLocationManager.updateById(toUpdate), "更新垃圾箱位置失败");
    }

    public void remove(Long id) {
        if (id != null && id > 0) {
            trashcanLocationManager.removeById(id);
        }
    }

    public void bind(TrashcanLocation param, String currentAccount, Date now) {
        Verify.isTrue(param.getId() != null && param.getId() > 0, "垃圾箱位置不存在");

        TrashcanLocation trashcanLocation = trashcanLocationManager.getById(param.getId());
        Verify.notNull(trashcanLocation, "垃圾箱位置不存在");

        DeviceBase deviceBase = deviceBaseManager.getbyImei(param.getImei());
        Verify.notNull(deviceBase, "设备不存在");

        TrashcanLocation toUpdate = TrashcanLocation.builder()
                .id(param.getId())
                .imei(param.getImei())
                .updateUser(currentAccount)
                .utime(now)
                .version(trashcanLocation.getVersion()).build();

        Verify.isTrue(trashcanLocationManager.updateById(toUpdate), "绑定垃圾箱位置失败");
    }

    public void unbind(TrashcanLocation param, String currentAccount, Date now) {
        Verify.isTrue(param.getId() != null && param.getId() > 0, "垃圾箱位置不存在");

        TrashcanLocation trashcanLocation = trashcanLocationManager.getById(param.getId());
        Verify.notNull(trashcanLocation, "垃圾箱位置不存在");


        TrashcanLocation toUpdate = TrashcanLocation.builder()
                .id(param.getId())
                .imei("")
                .updateUser(currentAccount)
                .utime(now)
                .version(trashcanLocation.getVersion()).build();

        Verify.isTrue(trashcanLocationManager.updateById(toUpdate), "绑定垃圾箱位置失败");

    }
}
